import './record.html';
import { Roles } from '/imports/api/roles/roles.js';


Template.data_record.onCreated(function(){

  
  if (this.data.record.username != undefined) {
    if (this.data.record.role_ids != undefined) {
       let role_ids = this.data.record.role_ids;
       
       for (var i = role_ids.length - 1; i >= 0; i--) {
        Meteor.subscribe('role.data', role_ids[i]);
        
       }


    }

  }


  

});




Template.data_record.helpers({

  columns(){
    let  columns = new Array();
    let isUser = false;
    for(key in this.record){
      
      if (this.record.hasOwnProperty(key)) {
        if (key == 'createdAt') {
  
          let deal_date = moment(this.record[key]).format('YYYY年MMMMDD日, HH:mm:ss');
          columns.push(deal_date);
          continue;
        }
        if (key == 'isBanned') {
          continue;
        }
        if (key == "_id") {
          continue;
        }
        if (key == 'username') {
          isUser = true;
        }
        columns.push(this.record[key]);
      }
      
    }



    if (isUser) {//对用户的列表进行特殊处理
      
        columns[1] = this.record.profile.mobile;
      if (this.record.role_ids == undefined) {
        columns = [
        ...columns.slice(0, 2),
        '暂无',
        ...columns.slice(3, 4)
       ];
      }else{

        let role_string = '';
        for (var i = this.record.role_ids.length - 1; i >= 0; i--) {
          let role = Roles.findOne(this.record.role_ids[i]);
          if (role !=undefined) {
             
            role_string = role_string  + role.name + ', '; 
          }
         
        }
        // 嵌入角色管理
        columns = [
        ...columns.slice(0, 2),
        role_string+'<div data-id="'+this.record._id+'" class="roles-manager btn btn-xs btn-info">管理角色</div>',
        ...columns.slice(3, 4)
       ];
        
        
        
      }
    }

    console.log(columns);
    
    return columns;
  },

  isUser(){
    if (this.record.username != undefined) {
      return true;
    }
    return false;
  },
  isActive(){
    
    if (this.record.isBanned != undefined) {
      if (this.record.isBanned) {
        return "";
      }
    }
    return 'checked';
  }
  
});


Template.data_record.onRendered(function () {

  $('.delete-checked').hide();
  $(".check-cust").iCheck({
     checkboxClass: "icheckbox_flat-red",
     radioClass: "iradio_flat-red"
   });
   if (this.subscriptionsReady()) {
     $('.check-all').iCheck('uncheck');
     $('.check-one').iCheck('uncheck');

  }

  $(".banned-user").bootstrapToggle(
    {
      on: '活动的',
      off: '已禁用'
    }
    );



});



Template.data_record.events({
  'click .delete'(event, instance){
    event.preventDefault();
    event.stopPropagation();
    if (confirm("是否确认删除?")) {
      UIBlock.block('正在删除');
      
      Meteor.call(this.modelmethods.delete, this.record._id, (error) => {
          if (error) {
            alert(error);
          } else {
              FlashMessages.sendSuccess("成功删除1条记录");
            UIBlock.unblock();
          }
        });
    }
  },

  'change .banned-user'(evnet, instance){
      event.preventDefault();
    event.stopPropagation();

    let state = this.record.isBanned;
    let confirmString = '';
   
    let resultStr = '';
    if (state) {resultStr = "激活"; }else{
      resultStr = "禁用";
    }
    
      UIBlock.block('正在'+resultStr+'该用户');
      
      Meteor.call('users.block', this.record._id, !state, (error) => {
          if (error) {
            alert(error);
          } else {
              FlashMessages.sendSuccess("成功"+resultStr+"1个用户");
            UIBlock.unblock();
          }
        });
    

  },

  'click .update'(event, instance){
    event.preventDefault();
    event.stopPropagation();
    Session.set("update_form", this.record);
    $('#edit-modal').modal('show');
  }

});
